{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 对print出的数据格式进行修正\n",
    "pd.set_option('expand_frame_repr', False)  # 当列太多时不换行\n",
    "pd.set_option('precision', 2)  # 浮点数的精度\n",
    "pd.set_option('display.max_rows', 10) # 显示的最大行数\n",
    "\n",
    "\n",
    "# 读入数据\n",
    "df = pd.read_csv(\n",
    "    # 该参数为数据在电脑中的路径，标题最好别带中文\n",
    "    filepath_or_buffer = r'C:\\notebooks\\quantitative_trading_notes\\data\\BITFINEX_BTCUSD_20180124_1T.csv',\n",
    "    # 该参数代表数据的分隔符，csv文件默认是逗号。其他常见的是'\\t'\n",
    "    sep=',',\n",
    "    # 该参数代表跳过数据文件的的第1行不读入\n",
    "    skiprows=1,\n",
    "    # nrows，只读取前n行数据，若不指定，读入全部的数据\n",
    "    # nrows=15,\n",
    "    # 将指定列的数据识别为日期格式。若不指定，时间数据将会以字符串形式读入。一开始先不用。\n",
    "    parse_dates=['candle_begin_time'],\n",
    "    # 将指定列设置为index。若不指定，index默认为0, 1, 2, 3, 4...\n",
    "    index_col=['candle_begin_time'],\n",
    "    # 读取指定的这几列数据，其他数据不读取。若不指定，读入全部列\n",
    "    # usecols=['candle_begin_time', 'close'],\n",
    "    # 当某行数据有问题时，报错。设定为False时即不报错，直接跳过该行。当数据比较脏乱的时候用这个。\n",
    "    # error_bad_lines=False,\n",
    "    # 将数据中的null识别为空值\n",
    "    # na_values='NULL',\n",
    "\n",
    "    # 更多其他参数，请直接搜索\"pandas read_csv\"，要去逐个查看一下。比较重要的，header等\n",
    "    # 还有read_table、read_excel、read_json等，他们的参数内容都是大同小异，可以自行搜索查看。\n",
    "\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "candle_begin_time\n",
      "2018-01-24 00:00:00    08点00分00秒\n",
      "2018-01-24 00:01:00    08点01分00秒\n",
      "2018-01-24 00:02:00    08点02分00秒\n",
      "2018-01-24 00:03:00    08点03分00秒\n",
      "2018-01-24 00:04:00    08点04分00秒\n",
      "                         ...    \n",
      "2018-01-24 23:55:00    07点55分00秒\n",
      "2018-01-24 23:56:00    07点56分00秒\n",
      "2018-01-24 23:57:00    07点57分00秒\n",
      "2018-01-24 23:58:00    07点58分00秒\n",
      "2018-01-24 23:59:00    07点59分00秒\n",
      "Name: 北京时间, Length: 1440, dtype: object\n",
      "candle_begin_time\n",
      "2018-01-24 00:00:00    2018年1月24日08点00分00秒\n",
      "2018-01-24 00:01:00    2018年1月24日08点01分00秒\n",
      "2018-01-24 00:02:00    2018年1月24日08点02分00秒\n",
      "2018-01-24 00:03:00    2018年1月24日08点03分00秒\n",
      "2018-01-24 00:04:00    2018年1月24日08点04分00秒\n",
      "                              ...         \n",
      "2018-01-24 23:55:00    2018年1月24日07点55分00秒\n",
      "2018-01-24 23:56:00    2018年1月24日07点56分00秒\n",
      "2018-01-24 23:57:00    2018年1月24日07点57分00秒\n",
      "2018-01-24 23:58:00    2018年1月24日07点58分00秒\n",
      "2018-01-24 23:59:00    2018年1月24日07点59分00秒\n",
      "Name: 北京时间, Length: 1440, dtype: object\n",
      "candle_begin_time\n",
      "2018-01-24 00:00:00    1.09e+06\n",
      "2018-01-24 00:01:00    1.09e+06\n",
      "2018-01-24 00:02:00    1.08e+06\n",
      "2018-01-24 00:03:00    1.08e+06\n",
      "2018-01-24 00:04:00    1.08e+06\n",
      "                         ...   \n",
      "2018-01-24 23:55:00    1.13e+06\n",
      "2018-01-24 23:56:00    1.13e+06\n",
      "2018-01-24 23:57:00    1.14e+06\n",
      "2018-01-24 23:58:00    1.14e+06\n",
      "2018-01-24 23:59:00    1.14e+06\n",
      "Name: close, Length: 1440, dtype: float64\n",
      "candle_begin_time\n",
      "2018-01-24 00:00:00    8.44e+05\n",
      "2018-01-24 00:01:00    6.53e+05\n",
      "2018-01-24 00:02:00    3.33e+05\n",
      "2018-01-24 00:03:00    2.18e+05\n",
      "2018-01-24 00:04:00    2.77e+05\n",
      "                         ...   \n",
      "2018-01-24 23:55:00    7.29e+05\n",
      "2018-01-24 23:56:00    6.46e+05\n",
      "2018-01-24 23:57:00    1.15e+06\n",
      "2018-01-24 23:58:00    9.82e+05\n",
      "2018-01-24 23:59:00    8.79e+05\n",
      "Length: 1440, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 行列加减乘除\n",
    "print(df['北京时间'])\n",
    "print('2018年1月24日' + df['北京时间'])  # 字符串列可以直接加上字符串，对整列进行操作,结果带着index，是series\n",
    "\n",
    "print(df['close'] * 100)  # 数字列直接加上或者乘以数字，对整列进行操作。\n",
    "print(df['close'] * df['volume'])  # 两列之间可以直接操作。收盘价*成交量，结果是series 带着index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>北京时间</th>\n",
       "      <th>北京时间2</th>\n",
       "      <th>交易所</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:00:00</th>\n",
       "      <td>10812.0</td>\n",
       "      <td>10895.00</td>\n",
       "      <td>10812.0</td>\n",
       "      <td>10886.00</td>\n",
       "      <td>77.54</td>\n",
       "      <td>08点00分00秒</td>\n",
       "      <td>2018年1月24日08点00分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:01:00</th>\n",
       "      <td>10886.0</td>\n",
       "      <td>10906.00</td>\n",
       "      <td>10871.0</td>\n",
       "      <td>10871.00</td>\n",
       "      <td>60.05</td>\n",
       "      <td>08点01分00秒</td>\n",
       "      <td>2018年1月24日08点01分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:02:00</th>\n",
       "      <td>10872.0</td>\n",
       "      <td>10872.00</td>\n",
       "      <td>10840.0</td>\n",
       "      <td>10840.00</td>\n",
       "      <td>30.69</td>\n",
       "      <td>08点02分00秒</td>\n",
       "      <td>2018年1月24日08点02分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:03:00</th>\n",
       "      <td>10840.0</td>\n",
       "      <td>10849.00</td>\n",
       "      <td>10815.0</td>\n",
       "      <td>10833.00</td>\n",
       "      <td>20.13</td>\n",
       "      <td>08点03分00秒</td>\n",
       "      <td>2018年1月24日08点03分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 00:04:00</th>\n",
       "      <td>10834.0</td>\n",
       "      <td>10853.00</td>\n",
       "      <td>10833.0</td>\n",
       "      <td>10847.00</td>\n",
       "      <td>25.56</td>\n",
       "      <td>08点04分00秒</td>\n",
       "      <td>2018年1月24日08点04分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:55:00</th>\n",
       "      <td>11284.0</td>\n",
       "      <td>11326.00</td>\n",
       "      <td>11284.0</td>\n",
       "      <td>11308.00</td>\n",
       "      <td>64.43</td>\n",
       "      <td>07点55分00秒</td>\n",
       "      <td>2018年1月24日07点55分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:56:00</th>\n",
       "      <td>11310.0</td>\n",
       "      <td>11347.00</td>\n",
       "      <td>11310.0</td>\n",
       "      <td>11346.11</td>\n",
       "      <td>56.94</td>\n",
       "      <td>07点56分00秒</td>\n",
       "      <td>2018年1月24日07点56分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:57:00</th>\n",
       "      <td>11346.0</td>\n",
       "      <td>11380.00</td>\n",
       "      <td>11338.0</td>\n",
       "      <td>11361.00</td>\n",
       "      <td>100.94</td>\n",
       "      <td>07点57分00秒</td>\n",
       "      <td>2018年1月24日07点57分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:58:00</th>\n",
       "      <td>11361.0</td>\n",
       "      <td>11427.00</td>\n",
       "      <td>11361.0</td>\n",
       "      <td>11407.00</td>\n",
       "      <td>86.06</td>\n",
       "      <td>07点58分00秒</td>\n",
       "      <td>2018年1月24日07点58分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-24 23:59:00</th>\n",
       "      <td>11410.0</td>\n",
       "      <td>11427.88</td>\n",
       "      <td>11391.0</td>\n",
       "      <td>11391.00</td>\n",
       "      <td>77.19</td>\n",
       "      <td>07点59分00秒</td>\n",
       "      <td>2018年1月24日07点59分00秒</td>\n",
       "      <td>bitfinex</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1440 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                        open      high      low     close  volume       北京时间                北京时间2       交易所\n",
       "candle_begin_time                                                                                          \n",
       "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54  08点00分00秒  2018年1月24日08点00分00秒  bitfinex\n",
       "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05  08点01分00秒  2018年1月24日08点01分00秒  bitfinex\n",
       "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69  08点02分00秒  2018年1月24日08点02分00秒  bitfinex\n",
       "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13  08点03分00秒  2018年1月24日08点03分00秒  bitfinex\n",
       "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56  08点04分00秒  2018年1月24日08点04分00秒  bitfinex\n",
       "...                      ...       ...      ...       ...     ...        ...                  ...       ...\n",
       "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43  07点55分00秒  2018年1月24日07点55分00秒  bitfinex\n",
       "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94  07点56分00秒  2018年1月24日07点56分00秒  bitfinex\n",
       "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94  07点57分00秒  2018年1月24日07点57分00秒  bitfinex\n",
       "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06  07点58分00秒  2018年1月24日07点58分00秒  bitfinex\n",
       "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19  07点59分00秒  2018年1月24日07点59分00秒  bitfinex\n",
       "\n",
       "[1440 rows x 8 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新增一列\n",
    "df['北京时间2'] = '2018年1月24日' + df['北京时间']\n",
    "df['交易所'] = 'bitfinex'\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10986.838615965982\n",
      "close     10986.84\n",
      "volume       30.46\n",
      "dtype: float64\n",
      "                        close  volume\n",
      "candle_begin_time                    \n",
      "2018-01-24 00:00:00  10886.00   77.54\n",
      "2018-01-24 00:01:00  10871.00   60.05\n",
      "2018-01-24 00:02:00  10840.00   30.69\n",
      "2018-01-24 00:03:00  10833.00   20.13\n",
      "2018-01-24 00:04:00  10847.00   25.56\n",
      "...                       ...     ...\n",
      "2018-01-24 23:55:00  11308.00   64.43\n",
      "2018-01-24 23:56:00  11346.11   56.94\n",
      "2018-01-24 23:57:00  11361.00  100.94\n",
      "2018-01-24 23:58:00  11407.00   86.06\n",
      "2018-01-24 23:59:00  11391.00   77.19\n",
      "\n",
      "[1440 rows x 2 columns]\n",
      "candle_begin_time\n",
      "2018-01-24 00:00:00    5481.77\n",
      "2018-01-24 00:01:00    5465.52\n",
      "2018-01-24 00:02:00    5435.35\n",
      "2018-01-24 00:03:00    5426.57\n",
      "2018-01-24 00:04:00    5436.28\n",
      "                        ...   \n",
      "2018-01-24 23:55:00    5686.21\n",
      "2018-01-24 23:56:00    5701.52\n",
      "2018-01-24 23:57:00    5730.97\n",
      "2018-01-24 23:58:00    5746.53\n",
      "2018-01-24 23:59:00    5734.09\n",
      "Length: 1440, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# mean求均值，默认参数axis = 0，axis代表方向，代表是对列进行mean，还是对行进行mean，实际中弄混很正常，到时候试一下就知道了。\n",
    "print(df['close'].mean())  # 求一整列的均值，返回一个数。显然是这整列的mean，会自动排除空值。\n",
    "print(df[['close', 'volume']].mean())  # 求两列的均值，返回两个数，Series\n",
    "print(df[['close', 'volume']])\n",
    "print(df[['close', 'volume']].mean(axis=1))  # 返回两列，是DataFrame。是横向的均值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11557.88958366\n",
      "10430.0\n",
      "232.04533055153308\n",
      "1440\n",
      "11016.0\n",
      "10793.71592129\n"
     ]
    }
   ],
   "source": [
    "# 几个描述数据的函数\n",
    "print(df['high'].max())  # 最大值\n",
    "print(df['low'].min())  # 最小值\n",
    "print(df['close'].std())  # 标准差\n",
    "print(df['close'].count())  # 非空的数据的数量\n",
    "print(df['close'].median())  # 中位数\n",
    "print(df['close'].quantile(0.25))  # 25%分位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                        close  close shift -1  close shift 1\n",
      "candle_begin_time                                           \n",
      "2018-01-24 00:00:00  10886.00        10871.00            NaN\n",
      "2018-01-24 00:01:00  10871.00        10840.00       10886.00\n",
      "2018-01-24 00:02:00  10840.00        10833.00       10871.00\n",
      "2018-01-24 00:03:00  10833.00        10847.00       10840.00\n",
      "2018-01-24 00:04:00  10847.00        10826.00       10833.00\n",
      "...                       ...             ...            ...\n",
      "2018-01-24 23:55:00  11308.00        11346.11       11283.00\n",
      "2018-01-24 23:56:00  11346.11        11361.00       11308.00\n",
      "2018-01-24 23:57:00  11361.00        11407.00       11346.11\n",
      "2018-01-24 23:58:00  11407.00        11391.00       11361.00\n",
      "2018-01-24 23:59:00  11391.00             NaN       11407.00\n",
      "\n",
      "[1440 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "# 删除某一列的方法\n",
    "del df['北京时间']\n",
    "del df['北京时间2']\n",
    "del df['交易所']\n",
    "\n",
    "# shift 函数\n",
    "df['close shift -1'] = df['close'].shift(-1)\n",
    "df['close shift 1'] = df['close'].shift(1)\n",
    "print(df[['close', 'close shift -1', 'close shift 1']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                        close     涨跌\n",
      "candle_begin_time                   \n",
      "2018-01-24 00:00:00  10886.00    NaN\n",
      "2018-01-24 00:01:00  10871.00 -15.00\n",
      "2018-01-24 00:02:00  10840.00 -31.00\n",
      "2018-01-24 00:03:00  10833.00  -7.00\n",
      "2018-01-24 00:04:00  10847.00  14.00\n",
      "...                       ...    ...\n",
      "2018-01-24 23:55:00  11308.00  25.00\n",
      "2018-01-24 23:56:00  11346.11  38.11\n",
      "2018-01-24 23:57:00  11361.00  14.89\n",
      "2018-01-24 23:58:00  11407.00  46.00\n",
      "2018-01-24 23:59:00  11391.00 -16.00\n",
      "\n",
      "[1440 rows x 2 columns]\n",
      "                        open      high      low     close  volume  close shift -1  close shift 1       涨跌幅\n",
      "candle_begin_time                                                                                         \n",
      "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54        10871.00            NaN       NaN\n",
      "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05        10840.00       10886.00 -1.38e-03\n",
      "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69        10833.00       10871.00 -2.85e-03\n",
      "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13        10847.00       10840.00 -6.46e-04\n",
      "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56        10826.00       10833.00  1.29e-03\n",
      "...                      ...       ...      ...       ...     ...             ...            ...       ...\n",
      "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43        11346.11       11283.00  2.22e-03\n",
      "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94        11361.00       11308.00  3.37e-03\n",
      "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94        11407.00       11346.11  1.31e-03\n",
      "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06        11391.00       11361.00  4.05e-03\n",
      "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19             NaN       11407.00 -1.40e-03\n",
      "\n",
      "[1440 rows x 8 columns]\n"
     ]
    }
   ],
   "source": [
    "# diff 计算涨跌幅\n",
    "df['涨跌'] = df['close'].diff(1)  # 求本行数据和上一行数据相减得到的值\n",
    "print(df[['close', '涨跌']])\n",
    "df.drop(['涨跌'], axis=1, inplace=True)  # 删除某一列的另外一种方式，inplace参数指是否替代原来的df\n",
    "df['涨跌幅'] = df['close'].pct_change(1)  # 类似于diff，但是求的是两个数直接的比例，相当于求涨跌幅\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                     volume  volume_cum\n",
      "candle_begin_time                      \n",
      "2018-01-24 00:00:00   77.54       77.54\n",
      "2018-01-24 00:01:00   60.05      137.59\n",
      "2018-01-24 00:02:00   30.69      168.28\n",
      "2018-01-24 00:03:00   20.13      188.42\n",
      "2018-01-24 00:04:00   25.56      213.97\n",
      "...                     ...         ...\n",
      "2018-01-24 23:55:00   64.43    43536.50\n",
      "2018-01-24 23:56:00   56.94    43593.44\n",
      "2018-01-24 23:57:00  100.94    43694.38\n",
      "2018-01-24 23:58:00   86.06    43780.44\n",
      "2018-01-24 23:59:00   77.19    43857.63\n",
      "\n",
      "[1440 rows x 2 columns]\n",
      "                        open      high      low     close  volume  close shift -1  close shift 1       涨跌幅  volume_cum   res\n",
      "candle_begin_time                                                                                                           \n",
      "2018-01-24 00:00:00  10812.0  10895.00  10812.0  10886.00   77.54        10871.00            NaN       NaN       77.54   NaN\n",
      "2018-01-24 00:01:00  10886.0  10906.00  10871.0  10871.00   60.05        10840.00       10886.00 -1.38e-03      137.59  1.00\n",
      "2018-01-24 00:02:00  10872.0  10872.00  10840.0  10840.00   30.69        10833.00       10871.00 -2.85e-03      168.28  1.00\n",
      "2018-01-24 00:03:00  10840.0  10849.00  10815.0  10833.00   20.13        10847.00       10840.00 -6.46e-04      188.42  1.00\n",
      "2018-01-24 00:04:00  10834.0  10853.00  10833.0  10847.00   25.56        10826.00       10833.00  1.29e-03      213.97  1.00\n",
      "...                      ...       ...      ...       ...     ...             ...            ...       ...         ...   ...\n",
      "2018-01-24 23:55:00  11284.0  11326.00  11284.0  11308.00   64.43        11346.11       11283.00  2.22e-03    43536.50  1.04\n",
      "2018-01-24 23:56:00  11310.0  11347.00  11310.0  11346.11   56.94        11361.00       11308.00  3.37e-03    43593.44  1.04\n",
      "2018-01-24 23:57:00  11346.0  11380.00  11338.0  11361.00  100.94        11407.00       11346.11  1.31e-03    43694.38  1.04\n",
      "2018-01-24 23:58:00  11361.0  11427.00  11361.0  11407.00   86.06        11391.00       11361.00  4.05e-03    43780.44  1.05\n",
      "2018-01-24 23:59:00  11410.0  11427.88  11391.0  11391.00   77.19             NaN       11407.00 -1.40e-03    43857.63  1.05\n",
      "\n",
      "[1440 rows x 10 columns]\n"
     ]
    }
   ],
   "source": [
    "# cum累加(cumulative)类函数\n",
    "df['volume_cum'] = df['volume'].cumsum()  # 该列的累加值\n",
    "print(df[['volume', 'volume_cum']])\n",
    "df['res'] = (df['涨跌幅'] + 1.0).cumprod()\n",
    "print(df)  # 该列的累乘值，此处计算的就是资金曲线，假设初始1元钱。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                        close  close_排名\n",
      "candle_begin_time                      \n",
      "2018-01-24 00:00:00  10886.00     473.0\n",
      "2018-01-24 00:01:00  10871.00     456.0\n",
      "2018-01-24 00:02:00  10840.00     428.5\n",
      "2018-01-24 00:03:00  10833.00     421.0\n",
      "2018-01-24 00:04:00  10847.00     438.0\n",
      "...                       ...       ...\n",
      "2018-01-24 23:55:00  11308.00    1359.0\n",
      "2018-01-24 23:56:00  11346.11    1393.0\n",
      "2018-01-24 23:57:00  11361.00    1400.5\n",
      "2018-01-24 23:58:00  11407.00    1423.0\n",
      "2018-01-24 23:59:00  11391.00    1416.0\n",
      "\n",
      "[1440 rows x 2 columns]\n",
      "10650.00    13\n",
      "11170.00    11\n",
      "11000.00    10\n",
      "11199.00     9\n",
      "11038.00     8\n",
      "            ..\n",
      "11105.00     1\n",
      "11045.00     1\n",
      "11271.00     1\n",
      "10989.79     1\n",
      "10547.00     1\n",
      "Name: close, Length: 715, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# =====其他列函数\n",
    "df['close_排名'] = df['close'].rank(ascending=True, pct=False)  # 输出排名。ascending参数代表是顺序还是逆序。pct参数代表输出的是排名还是排名比例\n",
    "print(df[['close', 'close_排名']])\n",
    "del df['close_排名']\n",
    "print(df['close'].value_counts())  # 计数。统计该列中每个元素出现的次数。返回的数据是Series"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
